﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.ComponentModel.Composition;
using System.ApplicationModel.Composition;
using System.Reflection;
using System.ComponentModel.Composition.Hosting;
using Anagramme.Services;
using System.ApplicationModel;
using System.Diagnostics;

namespace Anagramme
{
    /// <summary>
    /// Logique d'interaction pour App.xaml
    /// </summary>
    public partial class App : System.ApplicationModel.Application
    {
        /// <summary>
        /// Déclenche l'événement System.Windows.Application.Startup.
        /// </summary>
        /// <param name="e">System.Windows.StartupEventArgs qui contient les données d'événement.</param>
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            TraceSource traceSource = base.ApplicationLifetimeObjects.OfType<ITraceService>().Single().Source;
            traceSource.TraceEvent(TraceEventType.Start, 0, Anagramme.Resources.TraceStrings.APPLICATION_START);

            CompositionContainerService compositionService = base.ApplicationLifetimeObjects.OfType<CompositionContainerService>().Single();
            compositionService.Catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
            compositionService.Catalog.Catalogs.Add(new AssemblyCatalog(typeof(Anagramme.Views.IViewWorkflowManager).Assembly));
            compositionService.Catalog.Catalogs.Add(new AssemblyCatalog(typeof(Anagramme.Model.ILobbyModel).Assembly));

            compositionService.Container.GetShell<IWindow>().Show();
        }

        /// <summary>
        /// Déclenche l'événement System.Windows.Application.Exit.
        /// </summary>
        /// <param name="e">System.Windows.ExitEventArgs qui contient les données de l'événement.</param>
        protected override void OnExit(ExitEventArgs e)
        {
            TraceSource traceSource = base.ApplicationLifetimeObjects.OfType<ITraceService>().Single().Source;
            traceSource.TraceEvent(TraceEventType.Stop, 0, Anagramme.Resources.TraceStrings.APPLICATION_STOP);

            CompositionContainer container = base.ApplicationLifetimeObjects.OfType<CompositionContainerService>().Single().Container;
            IEnumerable<IDisposable> disposables = container.GetExportedValues<IDisposable>();

            foreach (IDisposable disposable in disposables)
            {
                disposable.Dispose();
            }
            
            base.OnExit(e);
        }
    }
}
